package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.DateUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.rabbitmq.model.MessageModel;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.dao.*;
import net.zigin.nhi.could.waste.model.HospitalDepart;
import net.zigin.nhi.could.waste.model.WasteBase;
import net.zigin.nhi.could.waste.rabbitmq.Producers;
import net.zigin.nhi.could.waste.service.WarnBaseService;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.model.WarnTime;
import net.zigin.nhi.could.waste.service.WarnTimeService;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-23 23:25:08                                               
##################################################################*/


/**
 * WarnTime服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: WarnTimeServiceImpl
 * @date 2021-9-23 23:25:08
 */
@Service
public class WarnTimeServiceImpl extends AbstractServiceImpl<WarnTime, String> implements WarnTimeService {
    @Autowired
    private WarnTimeMapper warnTimeMapper;
   /* @Autowired
    private Producer producer;*/
    @Autowired
    private WasteBaseMapper wasteBaseMapper;
    @Autowired
    private WarnBaseService warnBaseService;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;
    @Autowired
    private HospitalDepartMapper hospitalDepartMapper;
    @Autowired
    private Producers producers;

    @Override
    public BaseMapper<WarnTime, String> getMapper() {
        return warnTimeMapper;
    }


    @Override
    public void generateWarn(MessageModel messageModel) {
        // 获取医废 todo 应该是app端的操作
        WasteBase wasteBase = wasteBaseMapper.selectByPrimaryKey(messageModel.getMessage());
        // 判断医废状态 与 预警类型是否符合条件生成预警
        if (!wasteBase.getStatus().equals("out_depot") && !messageModel.getTitle().contains(wasteBase.getStatus())) {
            warnBaseService.generateWarn(wasteBase, messageModel);
        }
    }

    @Override
    public void generateCollectWarn(MessageModel messageModel) {
        String[] split = messageModel.getMessage().split(";");
        String wasteBaseId = split[0];
        String departId = split[1];
        HospitalDepart hospitalDepart = hospitalDepartMapper.selectByPrimaryKey(departId);
        WasteBase wasteBase = wasteBaseMapper.selectByPrimaryKey(wasteBaseId);
        if (wasteBase.getCreateTime().compareTo(hospitalDepart.getLastCollectTime()) == 0) {
            // 生成预警记录
            warnBaseService.generateCollectWarn(hospitalDepart, messageModel, wasteBase.getCreateTime());
        }
    }

    @Override
    public void sendWarnMessage(WasteBase wastebase) {
        // 获取医院ID
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        WarnTime warnTime = warnTimeMapper.getOneByHospitalBaseId(hospitalBaseId);
        // 根据医废类型状态日期判断规则
        String status = wastebase.getStatus();
        int weekNum = DateUtil.getWeekNum(); // 周天是0
        String warnType = "";
        int hour = 0;
        if (status.equals("do_collect")) {
            // 生成入库预警
            // 判断入库预警开关
            if (warnTime.getInYellowSwitch().equals("1")) {
                if (weekNum == 0) {
                    hour = warnTime.getInYellowValue7();
                } else if (weekNum == 1) {
                    hour = warnTime.getInYellowValue1();
                } else if (weekNum == 2) {
                    hour = warnTime.getInYellowValue2();
                } else if (weekNum == 3) {
                    hour = warnTime.getInYellowValue3();
                } else if (weekNum == 4) {
                    hour = warnTime.getInYellowValue4();
                } else if (weekNum == 5) {
                    hour = warnTime.getInYellowValue5();
                } else if (weekNum == 6) {
                    hour = warnTime.getInYellowValue6();
                }
                warnType = "in_depot_time_yellow";
                sendYellow(warnType, wastebase.getId(), hour * 60 * 60 * 1000L);
            }
            if (warnTime.getInRedSwitch().equals("1")) {
                if (weekNum == 0) {
                    hour = warnTime.getInRedValue7();
                } else if (weekNum == 1) {
                    hour = warnTime.getInRedValue1();
                } else if (weekNum == 2) {
                    hour = warnTime.getInRedValue2();
                } else if (weekNum == 3) {
                    hour = warnTime.getInRedValue3();
                } else if (weekNum == 4) {
                    hour = warnTime.getInRedValue4();
                } else if (weekNum == 5) {
                    hour = warnTime.getInRedValue5();
                } else if (weekNum == 6) {
                    hour = warnTime.getInRedValue6();
                }
                warnType = "in_depot_time_red";
                sendRed(warnType, wastebase.getId(), hour * 60 * 60 * 1000L);
            }
            // 更新科室上次收集时间
            HospitalDepart hospitalDepart = new HospitalDepart();
            hospitalDepart.setId(wastebase.getHospitalDepartId());
            hospitalDepart.setLastCollectTime(wastebase.getCreateTime());
            hospitalDepartMapper.updateByPrimaryKeySelective(hospitalDepart);
            // 判断时间 生成延迟消息
            if (warnTime.getCollectYellowSwitch().equals("1")) {
                if (weekNum == 0) {
                    hour = warnTime.getCollectYellowValue7();
                } else if (weekNum == 1) {
                    hour = warnTime.getCollectYellowValue1();
                } else if (weekNum == 2) {
                    hour = warnTime.getCollectYellowValue2();
                } else if (weekNum == 3) {
                    hour = warnTime.getCollectYellowValue3();
                } else if (weekNum == 4) {
                    hour = warnTime.getCollectYellowValue4();
                } else if (weekNum == 5) {
                    hour = warnTime.getCollectYellowValue5();
                } else if (weekNum == 6) {
                    hour = warnTime.getCollectYellowValue6();
                }
                warnType = "do_collect_time_yellow";
                sendCollectYellow(warnType, wastebase, hour * 60 * 60 * 1000L);
            }
            if (warnTime.getCollectRedSwitch().equals("1")) {
                if (weekNum == 0) {
                    hour = warnTime.getCollectRedValue7();
                } else if (weekNum == 1) {
                    hour = warnTime.getCollectRedValue1();
                } else if (weekNum == 2) {
                    hour = warnTime.getCollectRedValue2();
                } else if (weekNum == 3) {
                    hour = warnTime.getCollectRedValue3();
                } else if (weekNum == 4) {
                    hour = warnTime.getCollectRedValue4();
                } else if (weekNum == 5) {
                    hour = warnTime.getCollectRedValue5();
                } else if (weekNum == 6) {
                    hour = warnTime.getCollectRedValue6();
                }
                warnType = "do_collect_time_red";
                sendCollectRed(warnType, wastebase, hour * 60 * 60 * 1000L);
            }
        } else if (status.equals("in_depot")) {
            // 生成出库预警
            // 判断出库预警开关
            if (warnTime.getOutYellowSwitch().equals("1")) {
                if (weekNum == 0) {
                    hour = warnTime.getOutYellowValue7();
                } else if (weekNum == 1) {
                    hour = warnTime.getOutYellowValue1();
                } else if (weekNum == 2) {
                    hour = warnTime.getOutYellowValue2();
                } else if (weekNum == 3) {
                    hour = warnTime.getOutYellowValue3();
                } else if (weekNum == 4) {
                    hour = warnTime.getOutYellowValue4();
                } else if (weekNum == 5) {
                    hour = warnTime.getOutYellowValue5();
                } else if (weekNum == 6) {
                    hour = warnTime.getOutYellowValue6();
                }
                warnType = "out_depot_time_yellow";
                sendYellow(warnType, wastebase.getId(), hour * 60 * 60 * 1000L);
            }
            if (warnTime.getOutRedSwitch().equals("1")) {
                if (weekNum == 0) {
                    hour = warnTime.getOutRedValue7();
                } else if (weekNum == 1) {
                    hour = warnTime.getOutRedValue1();
                } else if (weekNum == 2) {
                    hour = warnTime.getOutRedValue2();
                } else if (weekNum == 3) {
                    hour = warnTime.getOutRedValue3();
                } else if (weekNum == 4) {
                    hour = warnTime.getOutRedValue4();
                } else if (weekNum == 5) {
                    hour = warnTime.getOutRedValue5();
                } else if (weekNum == 6) {
                    hour = warnTime.getOutRedValue6();
                }
                warnType = "out_depot_time_red";
                sendRed(warnType, wastebase.getId(), hour * 60 * 60 * 1000L);
            }
        }
    }

    private void sendYellow(String warnType, String wasteBaseId, Long delayTime) {
        if (delayTime <= 0) {
            return;
        }
        MessageModel messageModel = MessageModel.builder()
                .message(wasteBaseId)
                .title(warnType)
                .id(StringUtil.get32UUID())
                .build();
        producers.send(messageModel,warnType,delayTime);
    }

    private void sendRed(String warnType, String wasteBaseId, Long delayTime) {
        if (delayTime <= 0) {
            return;
        }
        MessageModel messageModel = MessageModel.builder()
                .message(wasteBaseId)
                .title(warnType)
                .id(StringUtil.get32UUID())
                .build();
        producers.send(messageModel,warnType,delayTime);
    }

    private void sendCollectYellow(String warnType, WasteBase wasteBase, Long delayTime) {
        if (delayTime <= 0) {
            return;
        }
        MessageModel messageModel = MessageModel.builder()
                .message(wasteBase.getId()+";"+wasteBase.getHospitalDepartId())
                .title(warnType)
                .id(StringUtil.get32UUID())
                .build();
        producers.send(messageModel,warnType,delayTime);
    }

    private void sendCollectRed(String warnType, WasteBase wasteBase, Long delayTime) {
        if (delayTime <= 0) {
            return;
        }
        MessageModel messageModel = MessageModel.builder()
                .message(wasteBase.getId()+";"+wasteBase.getHospitalDepartId())
                .title(warnType)
                .id(StringUtil.get32UUID())
                .build();
        producers.send(messageModel,warnType,delayTime);
    }

}